import 'package:get/get.dart';

/// 生命周期函数的钩子
/// onInit ： 初次渲染完毕，可以在这里定义控制器(输入框，顶部选项卡，PageView等)，调接口
/// onClose： 组件卸载时触发，清除定时器等

class ObxController extends GetxController {
  //如果需要使变量被观察的话，使用obs
  var count = 0.obs;
  final name = '小王'.obs;
  final items = <String>['吴亦凡', '潘玮柏', '罗志祥'].obs;
  increment() => count++;

  // ///getx的控制器
  // final _countGetx1 = 0.obs;
  // set countGetx1(value) => this._countGetx1.value = value;
  // get countGetx1 => this._countGetx1.value;

  // final _countGetx2 = 0.obs;
  // set countGetx2(value) => this._countGetx2.value = value;
  // get countGetx2 => this._countGetx2.value;

  // add1() => _countGetx1.value++;
  // add2() => _countGetx2.value++;
}

class CountController extends GetxController {
  ///getx的控制器
  final _count1 = 0.obs;
  set count1(value) => this._count1.value = value;
  get count1 => this._count1.value;

  final _count2 = 0.obs;
  set count2(value) => this._count2.value = value;
  get count2 => this._count2.value;

  add1() => _count1.value++;
  add2() => _count2.value++;
}

///getxbuilder 的控制器
class GetXBuilderController extends GetxController {
  final _count3 = 0.obs;
  set count3(value) => this._count3.value = value;
  get count3 => this._count3.value;

  final _count4 = 0.obs;
  set count4(value) => this._count4.value = value;
  get count4 => this._count4.value;

  add3() => _count3.value++;
  add4() => _count4.value++;
}

//依赖注入Get.put

class GetPutController extends GetxController {
  final _count = 0.obs;
  set count(value) => this._count.value = value;
  get count => this._count.value;

  add() => _count.value++;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    print('oninit');
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    print('onclose');
  }
}

///懒加载的controller

class LazyController extends GetxController {
  final _count = 0.obs;
  set count(value) => this._count.value = value;
  get count => this._count.value;

  add() => _count.value++;

  @override
  void onInit() {
    super.onInit();
    print("onInit");
  }

  @override
  void onClose() {
    super.onClose();
    print("onClose");
  }
}

///防抖限流 Workers
class DebounController extends GetxController {
  final _count = 0.obs;
  set count(value) => this._count.value = value;
  get count => this._count.value;

  add() => _count.value++;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    //每次 ---> 适合做监听、日志的收集
    ever(_count, (value) {
      print('每次 -> ' + value.toString());
    });
    //第一次
    once(_count, (value) {
      print('第一次 -> ' + value.toString());
    });

    //防抖两秒内  ---> 适合做搜索输入框
    debounce(_count, (value) {
      print('防抖两秒内 -> ' + value.toString());
    }, time: Duration(seconds: 2));

    //定时器
    interval(_count, (value) {
      print('定时器 -> ' + value.toString());
    }, time: Duration(seconds: 2));
  }
}
